/* 
T必须是某种对象 => T extends Object
K必须是T的一个key => K extends (keyof T)
*/
function getValue<T extends Object, K extends keyof T>(obj: T, key: K) {
    // 从对象中取索引
    return obj[key]
}


const obj = {
    name: "张三",
    age: 18,
    gender: '男'
}

// 提取obj中的所有key，结果是联合类型 "name" | "age" | "gender"
// type keys = keyof (typeof obj)

console.log(
    getValue(obj, 'gender')
);
console.log(
    getValue(obj, 'hobbies')
);




/*  */
interface Dad {
    zhengqian(): number
}

interface Mom {
    cook(): void
}

interface Child<T> extends Dad, Mom {
    play(toy: T): void
}

interface Gun {
    koujing: number
}

/* T是一个玩枪的孩子实例 */
class Student<T extends Child<Gun>> {
    child: T

    constructor(child: T) {
        this.child = child
    }

    testChild() {
        this.child.zhengqian()
        this.child.cook()
        this.child.play({ koujing: 8 })
    }
}